import {
    SAVE
} from './mutations_type';
import {
    getuserInfo
} from '@/API/user';
export default {
    namespaced: true,
    state: {
        uid: '',
        token: '',
        userInfo: '',
        hasLogin: false,
        liked_num: '',
        publish_num: ''
    },
    getters: {},
    mutations: {
        [SAVE](state, [key, value]) {
            state[key] = value
        },
        clear(state) {
            state['uid'] = ''
            state['token'] = ''
            state['userInfo'] = ''
            state['hasLogin'] = false
            state['publish_num'] = 0
            state['liked_num'] = 0
        },
    },
    actions: {
        getuserInfo({
            commit
        }, token) {
            return getuserInfo({
                token
            }).then(res => {
                console.log(res);
                let {
                    uid,
                    userInfo,
                    publish_num,
                    liked_num
                } = res
                // token没过期免登录
                commit('save', ['uid', uid])
                commit('save', ['userInfo', userInfo])
                commit('save', ['token', token])
                commit('save', ['hasLogin', true])
                commit('save', ['liked_num', liked_num])
                commit('save', ['publish_num', publish_num])
            })
        },
    }
}